/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package getsinanowdata;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 *
 * @author ryu
 */
public class GetSinaNowData {

    /**
     * 当天是否周六日
     *
     * @return trune 为交易日
     */
    public static boolean getWeekOfDate() {
        Calendar calendar = Calendar.getInstance();
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        //System.out.println("w="+w);
        return w > 0 && w < 6;
    }
    
   /**
     * 取得当天日期时间
     * @return int[0]=20170911   int[1]=113000
     */
    public static int[] getDate() {
        Calendar cal = Calendar.getInstance();// 取当前日期。
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        int[] dt = new int[2];
        dt[0] = (year * 10000 + month * 100 + day);
        dt[1] = hour * 10000 + minute * 100 + second;
        return dt;
    }

    /**
     * @param args the command line arguments
     * @throws java.lang.ClassNotFoundException
     * @throws java.sql.SQLException
     * @throws java.lang.InterruptedException
     */
    public static void main(String[] args) throws ClassNotFoundException, SQLException, InterruptedException {
        dbase db;
        if (args.length > 1) {
            db = new dbase(args[0].trim(), args[1].trim(), args[2].trim(), args[3].trim());
        } else {
            db = new dbase("127.0.0.1", "sa", "Ryuryuryu0804", "auto");
        }
        HtmlTools ht = new HtmlTools();
        int[] dt;
        while (true) {
            if (getWeekOfDate()) {   //是否为交易日
                dt = getDate();
                if ((dt[1] > 91500 && dt[1] < 113020) || (dt[1] > 125900 && dt[1] < 150020)) {
                    long test = System.currentTimeMillis();
                    List<String> ids = db.getGpids();
                    if (ids.size() > 0) {
                        db.insertData(ht.getGpdatas(ids));
                        db.analysis();
                        System.out.println("总时间:" + (System.currentTimeMillis() - test));
                    }
                }
            }
            Thread.sleep(500);
        }
    }

}
